package boot.demo.config;

import boot.demo.event.EventA;
import boot.demo.event.SimpleEventListener;
import boot.demo.event.SimpleEventListener02;
import com.google.common.eventbus.AsyncEventBus;
import com.google.common.eventbus.EventBus;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

@Configuration
public class EventBusConfig {

    private static Logger LOGGER = LoggerFactory.getLogger(EventBusConfig.class);

    @Autowired
    SimpleEventListener simpleEventListener;

    @Autowired
    SimpleEventListener02 simpleEventListener02;

    @Bean
    public AsyncEventBus createEventBus(){
        BlockingQueue blockingQueue = new LinkedBlockingQueue(200);
        ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(20, 200, 100, TimeUnit.MINUTES, blockingQueue);
        AsyncEventBus eventBus  = new AsyncEventBus(threadPoolExecutor, (exception, context) -> {
            LOGGER.error("exception ", exception);
        });
        eventBus.register(simpleEventListener);
        eventBus.register(simpleEventListener02);
        return eventBus;
    }
}
